package oakclub.utility;

import java.util.Calendar;
import java.util.Date;

public class DateUtility
{
    public static int calcAge(Date dateOfBirth) throws IllegalArgumentException
    {
        // check params
        if(dateOfBirth == null)
        {
            throw new IllegalArgumentException("dateOfBirth is null");
        }

        // init
        Calendar today = Calendar.getInstance();
        Calendar birthDate = Calendar.getInstance();
        birthDate.setTime(dateOfBirth);

        // birthdate must be before today
        if(birthDate.after(today))
        {
            throw new IllegalArgumentException("can't be born in the future");
        }

        return today.get(Calendar.YEAR) - birthDate.get(Calendar.YEAR);
    }

    public static boolean isBeforeToday(Date date)
    {
        Calendar today = Calendar.getInstance();
        today.set(Calendar.HOUR, 0);
        today.set(Calendar.MINUTE, 0);
        today.set(Calendar.SECOND, 0);

        return date.getTime() < today.getTime().getTime();
    }
}
